﻿using System;
using System.Globalization;
using System.Web;
using ThangNM_LIB.DB;
using ThangNM_LIB.EO;
using ThangNM_LIB.FUNC;
using System.IO;
using System.Net;
using System.Text;
using System.Web.Routing;

namespace CSharp
{
    /// <summary>
    /// Summary description for Resource
    /// </summary>

    public class Resource : IHttpHandler
    {
        //public class Resource : IRouteHandler//IHttpHandler
        //{
        //    public IHttpHandler GetHttpHandler(RequestContext requestContext)
        //    {
        //        ImageCroperHttpHandler imageCroperHandler = new ImageCroperHttpHandler();
        //        return imageCroperHandler;
        //    }

        //    private class ImageCroperHttpHandler : IHttpHandler
        //    {

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                String sRID = StringEx.Trim(Convert.ToString(context.Request.QueryString["sRID"]));
                if (!String.IsNullOrEmpty(sRID))
                {
                    KeyValueEO objKV = new KeyValueEO(null, sRID);
                    objKV = OtherEx.ConvertNumberToChar(objKV, 0);

                    if (CommonsCheck.KeyValueEO_OK(objKV))
                    {
                        ResourceEO objTemp = new ResourceEO();
                        objTemp.PK_iRID = Convert.ToInt64(objKV.K);
                        objTemp = (new ResourceDAO(false, ConstEx.connectionStrings.connMSSql_HD)).SelectItem(objTemp);
                        if (objTemp != null)
                        {
                            if (!String.IsNullOrEmpty(context.Request.Headers["If-Modified-Since"]))
                            {
                                CultureInfo provider = CultureInfo.InvariantCulture;
                                DateTime lastMod;
                                DateTime.TryParse(context.Request.Headers["If-Modified-Since"], out lastMod);
                                if (lastMod.Equals(objTemp.tDateTimeModify))
                                {
                                    context.Response.StatusCode = 304;
                                    context.Response.StatusDescription = "Not Modified";
                                    return;
                                }
                            }
                            HttpResponse r = context.Response;
                            r.ContentType = OtherEx.getHttp_ContentType(objTemp.iType);
                            if (!String.IsNullOrEmpty(StringEx.Trim(objTemp.sPath)))
                                r.WriteFile(objTemp.sPath);
                            else if (StringEx.CompareEx(objTemp.sUrl, "http://") || StringEx.CompareEx(objTemp.sUrl, "https://") || StringEx.CompareEx(objTemp.sUrl, "ftp://"))
                            {
                                try
                                {
                                    byte[] bTemp;
                                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(objTemp.sUrl);

                                    using (WebClient webClient = new WebClient())
                                    {
                                        WebProxy proxy = new WebProxy("proxy.fis.vn", 8080);
                                        proxy.Credentials = new NetworkCredential("fis\\ThangNM11", "Th@n9NM!!");
                                        proxy.UseDefaultCredentials = false;
                                        proxy.BypassProxyOnLocal = true;
                                        webClient.Proxy = proxy;
                                        bTemp = webClient.DownloadData(objTemp.sUrl);
                                    }
                                    r.OutputStream.Write(bTemp, 0, bTemp.Length);
                                }
                                catch { }
                            }
                            r.Cache.SetCacheability(HttpCacheability.Public);
                            r.Cache.SetLastModified(objTemp.tDateTimeModify);
                            return;
                        }
                    }
                }

                context.Response.StatusCode = 404;
                context.Response.StatusDescription = "Not Found";
                return;
            }
            catch (Exception ex)
            {

            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}